package test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

/**
 * @author WYC
 * @version 1.0
 * @ClassName FootballLeague
 * @date 2024/11/16 14:25
 * @description
 */
class Team {
    private String name; // 队名
    private int matchesPlayed; // 已比赛场数
    private int wins; // 胜场数
    private int draws; // 平场数
    private int losses; // 负场数
    private int goalsScored; // 进球数
    private int goalsConceded; // 失球数
    private int points; // 积分

    // 构造函数
    public Team(String name) {
        this.name = name;
    }

    // 更新球队数据
    public void updateStats(int scored, int conceded) {
        matchesPlayed++; // 比赛场次增加
        goalsScored += scored; // 更新进球数
        goalsConceded += conceded; // 更新失球数

        // 根据比分更新胜、平、负以及积分
        if (scored > conceded) {
            wins++;
            points += 3;
        } else if (scored == conceded) {
            draws++;
            points += 1;
        } else {
            losses++;
        }
    }

    // 计算净胜球
    public int getGoalDifference() {
        return goalsScored - goalsConceded;
    }

    // 获取球队名称
    public String getName() {
        return name;
    }

    // 获取积分
    public int getPoints() {
        return points;
    }

    // 获取进球数
    public int getGoalsScored() {
        return goalsScored;
    }

    // 重写 toString 方法，便于输出球队信息
    @Override
    public String toString() {
        return String.format(
                "%-15s %3d %3d %3d %3d %3d %3d %3d",
                name, matchesPlayed, wins, draws, losses, goalsScored, goalsConceded, points
        );
    }
}

public class FootballLeague {

    // 主方法
    public static void main(String[] args) {
        // 初始化球队列表
        List<Team> teams = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);

        // 输入球队信息
        System.out.println("请输入球队名称（不超过15个字符），输入'end'结束：");
        while (true) {
            String name = scanner.nextLine();
            if (name.equalsIgnoreCase("end")) break;

            // 检查队名长度
            if (name.length() >= 15) {
                System.out.println("队名过长，请输入不超过15个字符的队名！");
                continue;
            }

            teams.add(new Team(name));
        }

        // 输入比赛结果
        System.out.println("请输入比赛结果（主队名 客队名 主队进球 客队进球），输入'end'结束：");
        while (true) {
            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("end")) break;
            String[] parts = input.split(" ");
            if (parts.length != 4) {
                System.out.println("输入格式错误，请重新输入！");
                continue;
            }

            String homeTeamName = parts[0];
            String awayTeamName = parts[1];
            int homeScore = Integer.parseInt(parts[2]);
            int awayScore = Integer.parseInt(parts[3]);

            // 更新球队数据
            Team homeTeam = findTeam(teams, homeTeamName);
            Team awayTeam = findTeam(teams, awayTeamName);
            if (homeTeam != null && awayTeam != null) {
                homeTeam.updateStats(homeScore, awayScore);
                awayTeam.updateStats(awayScore, homeScore);
            } else {
                System.out.println("球队名称错误，请检查后重新输入！");
            }
        }

        // 排序并显示积分表
        sortTeams(teams);
        System.out.println("\n最新积分表：");
        System.out.printf("%-15s%3s%3s%3s %3s%3s%3s%3s\n", "队名", "场次", "胜", "平", "负", "进球", "失球", "积分");
        for (Team team : teams) {
            System.out.println(team);
        }

        scanner.close();
    }

    // 查找球队
    private static Team findTeam(List<Team> teams, String name) {
        for (Team team : teams) {
            if (team.getName().equalsIgnoreCase(name)) {
                return team;
            }
        }
        return null; // 未找到球队
    }

    // 排序方法
    private static void sortTeams(List<Team> teams) {
        teams.sort(Comparator.comparingInt(Team::getPoints).reversed()
                .thenComparingInt(Team::getGoalDifference).reversed()
                .thenComparingInt(Team::getGoalsScored).reversed());
    }
}